<script setup>
import {gameDetailEditAPI, gameGetAPI} from "@/api/AdminApi/game.js";
import {useRoute, useRouter} from "vue-router";
import {QuillEditor} from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import {onMounted, ref} from "vue";
import EditContainer from "@/components/AdminConponents/EditContainer.vue";
import {Plus} from "@element-plus/icons-vue";
import {ElMessage} from "element-plus";

const route = useRoute()
const gameDetailInfo = ref({})
const gameName = ref()
const gameId = ref()
const widerPicture = ref()
const higherPicture = ref()
const simplePicture = ref()
const post = ref()

const getGameViewInfo = async (id = route.params.id) => {
  const res = await gameGetAPI(id)
  gameDetailInfo.value = res.data.gameDetail
  gameName.value = res.data.chineseName
  gameId.value = res.data.id
}
onMounted(() => getGameViewInfo())

// 判断图片
const judgeFile = (file) => {
  // 检查文件是否为 JPG 格式
  if (file.type !== 'image/jpeg') {
    ElMessage.error('文件格式必须是jpg!')
    return false;
  } else if (file.size / 1024 / 1024 > 4) {
    ElMessage.error('文件大小不得超过 4MB!')
    return false
  }
}

// 横向图片
const widerPictureUpload = (uploadFile) => {
  const file = uploadFile.raw || uploadFile;
  judgeFile(file)
  // 文件是 JPG 格式，创建一个临时 URL
  widerPicture.value = file
  gameDetailInfo.value.widerPicture = URL.createObjectURL(file);
}

// 纵向图片
const higherPictureUpload = (uploadFile) => {
  const file = uploadFile.raw || uploadFile;
  judgeFile(file)
  // 文件是 JPG 格式，创建一个临时 URL
  higherPicture.value = file
  gameDetailInfo.value.higherPicture = URL.createObjectURL(file);
}

// 精简图片
const simplePictureUpload = (uploadFile) => {
  const file = uploadFile.raw || uploadFile;
  judgeFile(file)
  // 文件是 JPG 格式，创建一个临时 URL
  simplePicture.value = file
  gameDetailInfo.value.simplePicture = URL.createObjectURL(file);
}

// 海报
const postUpload = (uploadFile) => {
  const file = uploadFile.raw || uploadFile;
  judgeFile(file)
  // 文件是 JPG 格式，创建一个临时 URL
  post.value = file
  gameDetailInfo.value.post = URL.createObjectURL(file);
}

// 定义工具栏选项
const toolbarOptions = {
  container: [
    ['bold', 'italic', 'underline'],  // 加粗、斜体、下划线按钮
    [{'header': 1}, {'header': 2}],  // 标题1、标题2
    ['image']  // 插入图片按钮
  ]
};

// 取消按钮
const router = useRouter()
const cancel = () => {
  router.go(-1)
}

const formRef = ref()
// 修改按钮
const edit = async () => {
  await formRef.value.validate()
  const res = await gameDetailEditAPI(
      gameDetailInfo.value,
      widerPicture.value,
      higherPicture.value,
      simplePicture.value,
      post.value)
  if (res.code === 1) {
    ElMessage.success("更新成功")
    router.go(-1)
  } else {
    ElMessage.error(res.msg)
  }
}

</script>

<template>
  <EditContainer :title="`${gameName}游戏详细编辑`">
    <template #body>
      <el-form ref="formRef" :model="gameDetailInfo" label-width="auto" style="width: 1280px;">
        <el-form-item label="购买次数" prop="buyTimes">
          <el-input v-model="gameDetailInfo.buyTimes" disabled style="width: 200px;"></el-input>
        </el-form-item>
        <el-form-item label="横向图片" prop="widerPicture">
          <el-upload
              ref="uploadRef"
              :auto-upload="false"
              class="icon-uploader"
              :show-file-list="false"
              :on-change="widerPictureUpload">
            <el-image v-if="gameDetailInfo.widerPicture" :src="gameDetailInfo.widerPicture" class="widerPicture"/>
            <el-icon v-else class="icon-uploader-icon">
              <Plus/>
            </el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="纵向图片" prop="higherPicture">
          <el-upload
              ref="uploadRef"
              :auto-upload="false"
              class="icon-uploader"
              :show-file-list="false"
              :on-change="higherPictureUpload">
            <el-image v-if="gameDetailInfo.higherPicture" :src="gameDetailInfo.higherPicture" class="higherPicture"/>
            <el-icon v-else class="icon-uploader-icon">
              <Plus/>
            </el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="精简图片" prop="simplePicture">
          <el-upload
              ref="uploadRef"
              :auto-upload="false"
              class="icon-uploader"
              :show-file-list="false"
              :on-change="simplePictureUpload">
            <el-image v-if="gameDetailInfo.simplePicture" :src="gameDetailInfo.simplePicture" class="simplePicture"/>
            <el-icon v-else class="icon-uploader-icon">
              <Plus/>
            </el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="海报" prop="post">
          <el-upload
              ref="uploadRef"
              :auto-upload="false"
              class="icon-uploader"
              :show-file-list="false"
              :on-change="postUpload">
            <el-image v-if="gameDetailInfo.post" :src="gameDetailInfo.post" class="post"/>
            <el-icon v-else class="icon-uploader-icon">
              <Plus/>
            </el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="介绍" prop="introduction">
          <el-input
              v-model="gameDetailInfo.introduction"
              type="textarea"
              :rows="10"
              style="resize: none;"
          />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <div class="editor">
            <quill-editor
                ref="editorRef"
                v-model:content="gameDetailInfo.description"
                content-type="html"
                theme="snow"
                :toolbar="toolbarOptions"
            >
            </quill-editor>
          </div>
        </el-form-item>
      </el-form>
    </template>
    <template #footerLeft>
      <el-button class="button" @click="cancel" type="info">取消</el-button>
    </template>
    <template #footerRight>
      <el-button class="button" @click="edit" type="primary">修改</el-button>
    </template>
  </EditContainer>
</template>

<style scoped lang="scss">
.icon-uploader {
  :deep {
    .widerPicture {
      width: 460px;
      height: 215px;
      display: block;
    }

    .higherPicture {
      width: 300px;
      height: 450px;
      display: block;
    }

    .simplePicture {
      width: 186px;
      height: 69px;
      display: block;
    }

    .post {
      width: 616px;
      height: 353px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.icon-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 278px;
      height: 278px;
      text-align: center;
    }
  }
}

.editor {
  width: 100%;

  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>
